from toee import *

def san_dialog( attachee, triggerer ):
	if (not attachee.has_met( triggerer )):
		triggerer.begin_dialog( attachee, 1 )
	elif (attachee.leader_get() != OBJ_HANDLE_NULL):
		triggerer.begin_dialog( attachee, 420 )
	elif (game.global_flags[121] == 1 or game.global_flags[122] == 1 or game.global_flags[123] == 1):
		triggerer.begin_dialog( attachee, 150 )
	else:
		triggerer.begin_dialog( attachee, 160 )
	return SKIP_DEFAULT

def san_dying( attachee, triggerer ):
	game.global_flags[110] = 1
	if (attachee.leader_get() != OBJ_HANDLE_NULL):
		game.global_vars[29] = game.global_vars[29] + 1
	return RUN_DEFAULT

def san_resurrect( attachee, triggerer ):
	game.global_flags[110] = 0
	return RUN_DEFAULT

def san_start_combat( attachee, triggerer ):
	if (( obj_percent_hp(attachee) < 50 ) and (game.global_flags[350] == 0)):
		found_pc = OBJ_HANDLE_NULL
		for pc in game.party:
			if pc.type == obj_t_pc:
				found_pc = pc
				attachee.ai_shitlist_remove( pc )
		if found_pc != OBJ_HANDLE_NULL:
			game.global_flags[349] = 1
			found_pc.begin_dialog( attachee, 70 )
			return SKIP_DEFAULT
## THIS IS USED FOR BREAK FREE
	for obj in game.party[0].group_list():
		if (obj.distance_to(attachee) <= 9 and obj.stat_level_get(stat_hp_current) >= -9):
			return RUN_DEFAULT		
	while(attachee.item_find(8903) != OBJ_HANDLE_NULL):
		attachee.item_find(8903).destroy()
	if (attachee.d20_query(Q_Is_BreakFree_Possible)):
		create_item_in_inventory( 8903, attachee )
##		attachee.d20_send_signal(S_BreakFree)
	return RUN_DEFAULT

def san_heartbeat( attachee, triggerer ):
	if (not game.combat_is_active()):
		leader = attachee.leader_get()
		if (leader != OBJ_HANDLE_NULL):
			if (obj_percent_hp(attachee) > 70):
				if (group_percent_hp(leader) < 30):
					attachee.float_line(460,leader)
					attachee.attack(leader)
	return RUN_DEFAULT

def san_join( attachee, triggerer ):
##	game.global_flags[112] = 1		### removed by Livonya
	return RUN_DEFAULT

def TalkAern( attachee, triggerer, line):
	npc = find_npc_near(attachee,8033)
	if (npc != OBJ_HANDLE_NULL):
		triggerer.begin_dialog(npc,line)
		npc.turn_towards(attachee)
		attachee.turn_towards(npc)
	else:
		triggerer.begin_dialog(attachee,480)
	return SKIP_DEFAULT